using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class ChiTietDoanhThuTheoLoaiPhongRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //

		                                                          

		//////////////////////////////////////////////////////////

        public static ChiTietDoanhThuTheoLoaiPhongDTO CreateChiTietDoanhThuTheoLoaiPhongDTOFromChiTietDoanhThuTheoLoaiPhongEF(ChiTietDoanhThuTheoLoaiPhong objEf) // EF -> DTO
        {
            ChiTietDoanhThuTheoLoaiPhongDTO objDTO = new ChiTietDoanhThuTheoLoaiPhongDTO();
            Mapper.CreateMap<ChiTietDoanhThuTheoLoaiPhong, ChiTietDoanhThuTheoLoaiPhongDTO>();
            objDTO = Mapper.Map<ChiTietDoanhThuTheoLoaiPhong, ChiTietDoanhThuTheoLoaiPhongDTO>(objEf);
            return objDTO;
        }

        public static ChiTietDoanhThuTheoLoaiPhong CreateChiTietDoanhThuTheoLoaiPhongEFFromChiTietDoanhThuTheoLoaiPhongDTO(ChiTietDoanhThuTheoLoaiPhongDTO objDTO)    //DTO -> EF
        {
            ChiTietDoanhThuTheoLoaiPhong objEF = new ChiTietDoanhThuTheoLoaiPhong();
            Mapper.CreateMap<ChiTietDoanhThuTheoLoaiPhongDTO, ChiTietDoanhThuTheoLoaiPhong>();
            objEF = Mapper.Map<ChiTietDoanhThuTheoLoaiPhongDTO, ChiTietDoanhThuTheoLoaiPhong>(objDTO);
            return objEF;
        }

       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            int qr = (from x in Context.ChiTietDoanhThuTheoLoaiPhongList
                   select x.MaChiTietDoanhThu).Count();
            return qr;
        }

        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.ChiTietDoanhThuTheoLoaiPhongList
                        orderby x.MaChiTietDoanhThu descending
                        select x.MaChiTietDoanhThu;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(ChiTietDoanhThuTheoLoaiPhongDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                ChiTietDoanhThuTheoLoaiPhong objEF = CreateChiTietDoanhThuTheoLoaiPhongEFFromChiTietDoanhThuTheoLoaiPhongDTO(objDTO);
                context.AddToChiTietDoanhThuTheoLoaiPhongList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(ChiTietDoanhThuTheoLoaiPhongDTO objDTO)
        {
            try
            {
                ChiTietDoanhThuTheoLoaiPhong objEF = CreateChiTietDoanhThuTheoLoaiPhongEFFromChiTietDoanhThuTheoLoaiPhongDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.ChiTietDoanhThuTheoLoaiPhongList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<ChiTietDoanhThuTheoLoaiPhongDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.ChiTietDoanhThuTheoLoaiPhongList
                        orderby x.MaChiTietDoanhThu
                        select x).Skip(Start - 1).Take(Record);
            List<ChiTietDoanhThuTheoLoaiPhongDTO> lst = new List<ChiTietDoanhThuTheoLoaiPhongDTO>();
            foreach (var obj in query)
            {
                ChiTietDoanhThuTheoLoaiPhongDTO lp = ChiTietDoanhThuTheoLoaiPhongRepository.CreateChiTietDoanhThuTheoLoaiPhongDTOFromChiTietDoanhThuTheoLoaiPhongEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<ChiTietDoanhThuTheoLoaiPhongDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<ChiTietDoanhThuTheoLoaiPhongDTO> lst = new List<ChiTietDoanhThuTheoLoaiPhongDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from ChiTietDoanhThuTheoLoaiPhong in context.ChiTietDoanhThuTheoLoaiPhongList
                        select ChiTietDoanhThuTheoLoaiPhong;
            foreach (var objEf in query)
            {
                ChiTietDoanhThuTheoLoaiPhongDTO objDTO = CreateChiTietDoanhThuTheoLoaiPhongDTOFromChiTietDoanhThuTheoLoaiPhongEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public ChiTietDoanhThuTheoLoaiPhongDTO LayTheoMa(int MaChiTietDoanhThuTheoLoaiPhong)
        {
            ChiTietDoanhThuTheoLoaiPhongDTO objDTO = new ChiTietDoanhThuTheoLoaiPhongDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from ChiTietDoanhThuTheoLoaiPhong in context.ChiTietDoanhThuTheoLoaiPhongList
                            where ChiTietDoanhThuTheoLoaiPhong.MaChiTietDoanhThu == MaChiTietDoanhThuTheoLoaiPhong
                            select ChiTietDoanhThuTheoLoaiPhong;
                ChiTietDoanhThuTheoLoaiPhong objEF = query.First();
                objDTO = CreateChiTietDoanhThuTheoLoaiPhongDTOFromChiTietDoanhThuTheoLoaiPhongEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }

//
//Khoa ngoai DoanhThuTheoLoaiPhong
//
        public List<ChiTietDoanhThuTheoLoaiPhongDTO> LayDanhSachTheoMaDoanhThuTheoLoaiPhong( int MaDoanhThuTheoLoaiPhong)
        {
            List<ChiTietDoanhThuTheoLoaiPhongDTO> lst = new List<ChiTietDoanhThuTheoLoaiPhongDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from ChiTietDoanhThuTheoLoaiPhong in context.ChiTietDoanhThuTheoLoaiPhongList
                         where ChiTietDoanhThuTheoLoaiPhong.MaDoanhThu == MaDoanhThuTheoLoaiPhong 
                        select ChiTietDoanhThuTheoLoaiPhong;
                 foreach (var objEf in query)
                 {
                      ChiTietDoanhThuTheoLoaiPhongDTO objDTO = CreateChiTietDoanhThuTheoLoaiPhongDTOFromChiTietDoanhThuTheoLoaiPhongEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaDoanhThuTheoLoaiPhong( int MaDoanhThuTheoLoaiPhong, bool XoaKhoaNgoai=false)
        {
            List<ChiTietDoanhThuTheoLoaiPhongDTO> lst = this.LayDanhSachTheoMaDoanhThuTheoLoaiPhong( MaDoanhThuTheoLoaiPhong);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaChiTietDoanhThu;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }


//
//Khoa ngoai LoaiPhong
//
        public List<ChiTietDoanhThuTheoLoaiPhongDTO> LayDanhSachTheoMaLoaiPhong( int MaLoaiPhong)
        {
            List<ChiTietDoanhThuTheoLoaiPhongDTO> lst = new List<ChiTietDoanhThuTheoLoaiPhongDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from ChiTietDoanhThuTheoLoaiPhong in context.ChiTietDoanhThuTheoLoaiPhongList
                         where ChiTietDoanhThuTheoLoaiPhong.MaLoaiPhong == MaLoaiPhong 
                        select ChiTietDoanhThuTheoLoaiPhong;
                 foreach (var objEf in query)
                 {
                      ChiTietDoanhThuTheoLoaiPhongDTO objDTO = CreateChiTietDoanhThuTheoLoaiPhongDTOFromChiTietDoanhThuTheoLoaiPhongEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaLoaiPhong( int MaLoaiPhong, bool XoaKhoaNgoai=false)
        {
            List<ChiTietDoanhThuTheoLoaiPhongDTO> lst = this.LayDanhSachTheoMaLoaiPhong( MaLoaiPhong);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaChiTietDoanhThu;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }

        public bool Xoa(int MaChiTietDoanhThuTheoLoaiPhong, bool XoaKhoaNgoai=false)
        {
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                }
                QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();
                var query = from x in context.ChiTietDoanhThuTheoLoaiPhongList
                            where x.MaChiTietDoanhThu == MaChiTietDoanhThuTheoLoaiPhong 
                            select x;
                if(query.First()!= null)
                {
                    context.DeleteObject(query.First());
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }
////////////////////////////////////////////////////////////////
    }
}

